﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SalePartial.BLL;
using SalePartial.Model;

public partial class CategoryList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
               
            Guid caid;
            string cid = Request.QueryString["caid"];
            string page = Request.QueryString["page"];
            if (string.IsNullOrEmpty(page))
            {
                Session["sKey"] = null;
                Session["caSelect"] = null;
            }
            this.dptCategory.DataSource = new XPNewsCateGory().SelectDynamic(null, null);
            dptCategory.DataTextField = "CaName";
            dptCategory.DataValueField = "CaID";
            dptCategory.DataBind();
            dptCategory.Items.Insert(0, new ListItem("所有类别", "0"));

            if (cid != null && cid != string.Empty)
            {
                caid = new Guid(cid);

                //XPNews nm = new XPNews();
                XPNewsCateGoryInfo info = new XPNewsCateGory().GetXPNewsCateGory(caid);

                if (info !=null)
                {
                    lblCaName.Text = info.CaName;
                }
                else
                {
                    lblCaName.Text = "所属分类"; 
                }

                BindNews();

            }
        }
    }


    protected void BindNews()
    {
        string strWhere, searchkey, caid;
        searchkey="";
        caid="0";
        if (Session["sKey"]!=null)
        {
            searchkey = Session["sKey"].ToString();
        }
        if (Session["caSelect"]!=null)
        {
            caid = Session["caSelect"].ToString();
        }
        string cid = Request.QueryString["caid"];

        strWhere = " Title like '%" + searchkey + "%'";
        if (!string.IsNullOrEmpty(searchkey))
        {
            if (caid != "0")
            {
                strWhere += " and CaID=" + new Guid(caid);
            }
        }
        else
        {
            if (cid != null && cid != string.Empty)
            {

                strWhere += " and CaID=" + new Guid(cid);
            }
        }

        //AspNetPager1.RecordCount = new XPNews().SelectDynamic(strWhere, null);

        rptNewNews.DataSource = new XPNews().SelectDynamic(strWhere,null);
        rptNewNews.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        // Response.Write("sKey=" + Session["sKey"] + "<br>");
        // Response.Write("caSelect=" + Session["caSelect"]);
        BindNews();

    }

    protected void btnS_Click(object sender, EventArgs e)
    {
        string cid = Request.QueryString["caid"];
        Session["sKey"]=txtKey.Text.Trim();
        Session["caSelect"]=dptCategory.SelectedValue;

        BindNews();

    }

}

